$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/buttonevent/list2',
        datatype: "json",
        colModel: [
			{ label: 'ID ', name: 'id', index: 'id', align: "center", width: 60, key: true, hidden: true },
			{ label: '用户ID ', name: 'userid', index: 'userid', align: "center", width: 60,
				formatter:function(value,options,rowData){
					return "<a  onclick='window.open(\"../sys/buserinfo.html?userId="+value+"\")'>"+value+"</a>";
				} },
			{ label: '用户名称 ', name: 'userName', index: 'userName', align: "center", width: 60 },
			{ label: '事件代码', name: 'eventcode', index: 'eventcode', align: "center", width: 60, hidden: true },
			{ label: '事件名称', name: 'eventname', index: 'eventname', align: "center", width: 100 },
			{ label: '公众号 ', name: 'gzhName', index: 'gzhName', align: "center", width: 60 },
			{ label: '创建时间 ', name: 'createtime', index: 'createtime', align: "center", width: 100 },
			{ label: '跟进状态', name: 'followstatus', index: 'followstatus', align: "center", width: 60, formatter(value,options,rowData) {
					if (value == 0) {
						return "<p style='color: red'>未跟进</p>";
					} else if (value == 1) {
						return "<p style='color: blue'>已跟进</p>";
					}
				}
			},
			{ label: '跟进人ID', name: 'followuserid', index: 'followuserid', align: "center", width: 80, hidden: true },
			{ label: '跟进人', name: 'followusername', index: 'followusername', align: "center", width: 80 },
			{ label: '跟进时间', name: 'followtime', index: 'followtime', align: "center", width: 90 },
			{ label: '操作', name: '', index: '', align: "center", width: 80, formatter(value,options,rowData) {
					let id = rowData["id"];
					let followStatus = rowData["followstatus"];
					let content = "";
					if (followStatus == 0) {
						content += "<a class='btn btn-primary' onclick='vm.getInfoById(\""+id+"\","+followStatus+")'>跟进</a>";
					} else if (followStatus == 1) {
						content += "<a class='btn btn-primary' onclick='vm.getInfoById(\""+id+"\","+followStatus+")'>详情</a>";
					}
					return content;
				}
			},
        ],
		viewrecords: true,
        height: "100%",
        rowNum: 1000,
		rowList : [1000,2000,3000],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        // pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
		postData:{"userId": vm.q.userId, "gzhCode": vm.q.gzhCode, "eventCode": vm.q.eventCode, "startDate": vm.q.startDate, "endDate": vm.q.endDate},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
		beforeRequest: function() {
        	$("thead th").css("text-align","center");
			$("#jqgh_jqGrid_cb").css("text-align","left");// 表头checkbox居左
		}
    });
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/buserinfo/getUserGzhList',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				//vm.bbrandinfo=r.brandInfo;
				$("#selectUserGzhList").append(
					"<option value=''>全部平台</option>");
				for(var i=0;i<r.userGzhList.length;i++){
					$("#selectUserGzhList").append(
						"<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
				}
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			userId: null,
			gzhCode: '',
			eventCode: '',
			startDate: null,
			endDate: null,
		},
		showList: true,
		title: null,
		buttonEventInfo: {}
	},
	// 页面加载完毕后触发的事件
	mounted() {
		var vm = this;
		vm.q.startDate = vm.todayDate(); // 默认显示为当天时间
		vm.q.endDate = vm.todayDate();
	},
	methods: {
		query: function () {
			if (!vm.q.gzhCode || vm.q.gzhCode == '') {
				alert("请选择平台");
				return;
			}
			// if (!vm.q.eventCode || vm.q.eventCode == '') {
			// 	alert("请选择事件");
			// 	return;
			// }
			vm.reload();
		},
		todayDate: function() {
			let now = new Date();

			let y = now.getFullYear(); //年
			let m = now.getMonth() + 1 + ''; //月
			let d = now.getDate() + ''; //日

			m = m.length == 1 ? '0' + m : m;
			d = d.length == 1 ? '0' + d : d;
			return y + '-' + m + '-' + d;
		},
		getInfoById: function (id, status) {
			vm.title = "修改";
			vm.showList = false;

			$.ajax({
				type: "POST",
				url: baseURL + "sys/buttonevent/info/" + id,
				contentType: "application/json;charset=utf-8",
				success: function (r) {
					if (r.code === 0) {
						vm.buttonEventInfo = r.buttonEventInfo;
						if (status == 1) {
							$("#followButton").attr("disabled", true);
						} else {
							$("#followButton").attr("disabled", false);
						}
					} else {
						alert(r.msg);
					}
				}
			});
		},
		updateFollowStatus: function () {
			$.ajax({
				type: "POST",
				url: baseURL + "sys/buttonevent/updateById",
				contentType: "application/json;charset=utf-8",
				data: JSON.stringify(vm.buttonEventInfo),
				success: function (r) {
					if (r.code === 0) {
						vm.reload();
					} else {
						alert(r.msg);
					}
				}
			});
		},
		cancle: function () {
			vm.showList = true;
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{"userId": vm.q.userId, "gzhCode": vm.q.gzhCode, "eventCode": vm.q.eventCode, "startDate": vm.q.startDate, "endDate": vm.q.endDate},
                page:page 
            }).trigger("reloadGrid");
		}
	}
});